/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Supplier.java
 *
 * Created on Oct 30, 2013, 8:27:44 PM
 */
package sims.gui;

import java.util.List;
import java.util.Vector;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import sims.dao.SupplierDao;
import sims.entity.TbSupplier;
import sims.library.Validate;

/**
 *
 * @author NINH
 */
public class Supplier extends javax.swing.JPanel {

    SupplierDao supDao;
    Validate valid;

    /** Creates new form Supplier */
    public Supplier() {
        initComponents();
        supDao = new SupplierDao();
        valid = new Validate();
        loadData();
        disableTextbox();
    }

    public DefaultTableModel DisplayAll() {
        Vector vtable = new Vector();
        Vector vrow = new Vector();
        Vector vheader = new Vector();
        List<TbSupplier> suppliers = supDao.getAll(TbSupplier.class);

        vheader.add("Id");
        vheader.add("Name");
        vheader.add("Address");
        vheader.add("Phone");

        for (TbSupplier sup : suppliers) {
            vrow.add(sup.getSupId());
            vrow.add(sup.getSupName());
            vrow.add(sup.getSupAddress());
            vrow.add(sup.getSupPhone());
            vtable.add(vrow);
            vrow = new Vector();
        }
        return new DefaultTableModel(vtable, vheader);
    }

    public void loadData() {
        tblSupplier.setModel(DisplayAll());
    }

    public void Reset() {
        txtSupAddress.setText("");
        txtSupName.setText("");
        txtSupId.setText("");
        txtSupPhone.setText("");
    }

    public void binding() {
        int row = tblSupplier.getSelectedRow();
        String id = tblSupplier.getValueAt(row, 0).toString();
        String name = tblSupplier.getValueAt(row, 1).toString();
        String address = tblSupplier.getValueAt(row, 2).toString();
        String phone = tblSupplier.getValueAt(row, 3).toString();

        txtSupId.setText(id);
        txtSupName.setText(name);
        txtSupAddress.setText(address);
        txtSupPhone.setText(phone);
    }

    public void disableTextbox() {
        txtSupName.setEnabled(false);
        txtSupAddress.setEnabled(false);
        txtSupPhone.setEnabled(false);
    }

    public void enableTextbox() {
        txtSupName.setEnabled(true);
        txtSupAddress.setEnabled(true);
        txtSupPhone.setEnabled(true);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tblSupplier = new javax.swing.JTable();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txtSupName = new javax.swing.JTextField();
        txtSupAddress = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        txtSupPhone = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        btnDelete = new javax.swing.JButton();
        btnUpdate = new javax.swing.JButton();
        btnAdd = new javax.swing.JButton();
        lblSupName = new javax.swing.JLabel();
        lblSupAddress = new javax.swing.JLabel();
        lblSupPhone = new javax.swing.JLabel();
        txtSupId = new javax.swing.JTextField();
        lblSupId = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();

        setBackground(new java.awt.Color(255, 255, 255));
        setBorder(javax.swing.BorderFactory.createTitledBorder("Supplier manager"));
        setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        tblSupplier.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(tblSupplier);

        add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 20, 1030, 250));

        jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel1.setText("Supplier name: ");
        jPanel1.add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 40, 110, 18));
        jPanel1.add(txtSupName, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 40, 270, -1));
        jPanel1.add(txtSupAddress, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 70, 270, -1));

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel2.setText("Supplier address: ");
        jPanel1.add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 70, 110, 18));
        jPanel1.add(txtSupPhone, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 100, 270, -1));

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel3.setText("Supplier phone: ");
        jPanel1.add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 100, 110, 18));

        btnDelete.setIcon(new javax.swing.ImageIcon(getClass().getResource("/sims/images/delete.png"))); // NOI18N
        btnDelete.setText("Delete");
        btnDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeleteActionPerformed(evt);
            }
        });
        jPanel1.add(btnDelete, new org.netbeans.lib.awtextra.AbsoluteConstraints(300, 130, -1, 24));

        btnUpdate.setIcon(new javax.swing.ImageIcon(getClass().getResource("/sims/images/edit.png"))); // NOI18N
        btnUpdate.setText("Edit");
        btnUpdate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnUpdateActionPerformed(evt);
            }
        });
        jPanel1.add(btnUpdate, new org.netbeans.lib.awtextra.AbsoluteConstraints(210, 130, -1, 24));

        btnAdd.setIcon(new javax.swing.ImageIcon(getClass().getResource("/sims/images/add.png"))); // NOI18N
        btnAdd.setText("Add");
        btnAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddActionPerformed(evt);
            }
        });
        jPanel1.add(btnAdd, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 130, -1, 24));

        lblSupName.setFont(new java.awt.Font("Tahoma", 2, 11));
        lblSupName.setForeground(new java.awt.Color(255, 0, 0));
        jPanel1.add(lblSupName, new org.netbeans.lib.awtextra.AbsoluteConstraints(420, 40, 510, 20));

        lblSupAddress.setFont(new java.awt.Font("Tahoma", 2, 11));
        lblSupAddress.setForeground(new java.awt.Color(255, 0, 0));
        jPanel1.add(lblSupAddress, new org.netbeans.lib.awtextra.AbsoluteConstraints(420, 70, 510, 20));

        lblSupPhone.setFont(new java.awt.Font("Tahoma", 2, 11));
        lblSupPhone.setForeground(new java.awt.Color(255, 0, 0));
        jPanel1.add(lblSupPhone, new org.netbeans.lib.awtextra.AbsoluteConstraints(420, 100, 510, 20));

        txtSupId.setEnabled(false);
        jPanel1.add(txtSupId, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 10, 270, -1));

        lblSupId.setFont(new java.awt.Font("Tahoma", 2, 11));
        lblSupId.setForeground(new java.awt.Color(255, 0, 0));
        jPanel1.add(lblSupId, new org.netbeans.lib.awtextra.AbsoluteConstraints(420, 10, 510, 20));

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel4.setText("Supplier Id: ");
        jPanel1.add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 10, 110, 18));

        add(jPanel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 280, 1030, 265));
    }// </editor-fold>//GEN-END:initComponents

    private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddActionPerformed
        // TODO add your handling code here:
        if (btnAdd.getText().equalsIgnoreCase("add")) {
            enableTextbox();
            Reset();
            btnAdd.setText("Save");
            tblSupplier.setEnabled(false);
            btnUpdate.setEnabled(false);
            btnDelete.setText("Cancel");
        } else {
            try {
                String name = txtSupName.getText();
                String address = txtSupAddress.getText();
                String phone = txtSupPhone.getText();

                TbSupplier sup = new TbSupplier();
                sup.setSupName(name);
                sup.setSupAddress(address);
                sup.setSupPhone(phone);

                supDao.insert(sup);
                JOptionPane.showMessageDialog(null, "Insert success.");
            } catch (Exception e) {
                JOptionPane.showMessageDialog(null, "Insert error.");
            }
            loadData();
            Reset();
            disableTextbox();
            btnAdd.setText("Add");
            tblSupplier.setEnabled(true);
            btnUpdate.setEnabled(true);
            btnDelete.setText("Delete");
        }
    }//GEN-LAST:event_btnAddActionPerformed

    private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeleteActionPerformed
        // TODO add your handling code here:
        if (btnDelete.getText().equalsIgnoreCase("delete")) {
            if (tblSupplier.getSelectedRow() == -1) {
                JOptionPane.showMessageDialog(null, "Select supplier to delete.");
                return;
            } else {
                int check = JOptionPane.showConfirmDialog(null, "Are you sure");
                if (check == JOptionPane.OK_OPTION) {
                    int rowIndex = tblSupplier.getSelectedRow();
                    int supId = Integer.parseInt(tblSupplier.getValueAt(rowIndex, 0).toString());
                    TbSupplier sup = (TbSupplier) supDao.getById(TbSupplier.class, supId);
                    supDao.delete(sup);
                    JOptionPane.showMessageDialog(null, "Delete success.");
                    loadData();
                }
            }
        } else {
            tblSupplier.setEnabled(true);
            btnAdd.setText("Add");
            btnAdd.setEnabled(true);
            btnUpdate.setText("Edit");
            btnUpdate.setEnabled(true);
            btnDelete.setText("Delete");
            Reset();
            disableTextbox();
        }
    }//GEN-LAST:event_btnDeleteActionPerformed

    private void btnUpdateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnUpdateActionPerformed
        // TODO add your handling code here:
        if (btnUpdate.getText().equalsIgnoreCase("edit")) {
            if (tblSupplier.getSelectedRow() == -1) {
                JOptionPane.showMessageDialog(null, "Select supplier to edit.");
                return;
            } else {
                enableTextbox();
                binding();
                btnUpdate.setText("Save");
                tblSupplier.setEnabled(false);
                btnAdd.setEnabled(false);
                btnDelete.setText("Cancel");
            }
        } else {
            int id = Integer.parseInt(txtSupId.getText());
            String name = txtSupName.getText();
            String address = txtSupAddress.getText();
            String phone = txtSupPhone.getText();

            TbSupplier sup = new TbSupplier();
            sup.setSupId(id);
            sup.setSupName(name);
            sup.setSupAddress(address);
            sup.setSupPhone(phone);

            supDao.update(sup);
            JOptionPane.showMessageDialog(null, "Update success.");
            loadData();
            Reset();
            disableTextbox();

            btnUpdate.setText("Edit");
            tblSupplier.setEnabled(true);
            btnAdd.setEnabled(true);
            btnDelete.setText("Delete");
        }
    }//GEN-LAST:event_btnUpdateActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAdd;
    private javax.swing.JButton btnDelete;
    private javax.swing.JButton btnUpdate;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblSupAddress;
    private javax.swing.JLabel lblSupId;
    private javax.swing.JLabel lblSupName;
    private javax.swing.JLabel lblSupPhone;
    private javax.swing.JTable tblSupplier;
    private javax.swing.JTextField txtSupAddress;
    private javax.swing.JTextField txtSupId;
    private javax.swing.JTextField txtSupName;
    private javax.swing.JTextField txtSupPhone;
    // End of variables declaration//GEN-END:variables
}
